<template>
	<view>
		<div class='mapViewer' id='mapViewer'></div>
	</view>
</template>

<script lang="ts">
	import { onMounted } from 'vue'
	import * as Cesium from 'cesium'
	//import buildModuleUrl from '@/static/Cesium/Core/buildModuleUrl.js'

    import ProvinceView from './ProvinceView'
	import CountryView from './CountryView'
	import GlobeBase from './GlobeBase'
	export default {
		data() {
			return {

			}
		},
		onInit() {

		},

		setup() {
			onMounted(() => {
				mapInit()
			})
			
			let globeBase: GlobeBase|null = null
			let provinceView: ProvinceView|null = null
			let countryView: CountryView|null = null
			
			async function mapInit() {
				const CESIUM_BASE_URL = '/static/Cesium';
				(window as any).CESIUM_BASE_URL = CESIUM_BASE_URL
				//buildModuleUrl.setBaseUrl(CESIUM_BASE_URL)
				Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJhMGUwYmM4YS04NTMwLTQwNWYtOTU5My1kZDllZDhkYTM1MmIiLCJpZCI6NjIzNTQsImlhdCI6MTc0NTcxOTYxNH0.WtdyhaBGzubfRx0nc3__TbSJeqPIg4cW7UCzyfmPfOM'
				Cesium.Ion.defaultServer = CESIUM_BASE_URL
				const viewer = new Cesium.Viewer('mapViewer', {
					baseLayerPicker: false,
					fullscreenButton: false,
					vrButton: false,
					geocoder: false,
					homeButton: false,
					infoBox: false,
					sceneModePicker: false,
					selectionIndicator: false,
					shouldAnimate:false,
					timeline: false,
					animation: false,
					navigationHelpButton: false,
					navigationInstructionsInitiallyVisible: false,
					// sceneMode: Cesium.SceneMode.COLUMBUS_VIEW,
					msaaSamples:8,
					//terrainProvider: await Cesium.CesiumTerrainProvider.fromUrl('http://localhost/terrain/'),
					baseLayer: new Cesium.ImageryLayer(new Cesium.UrlTemplateImageryProvider({
						//url: CESIUM_BASE_URL + '/Assets/Textures/NaturalEarthII/{z}/{x}/{reverseY}.jpg',
						url: '/static/gisData/kejimin.png',
						tilingScheme: new Cesium.GeographicTilingScheme({}),
						maximumLevel: 18
					}))
				})
				
				//初始化相机位置
				viewer.scene.camera.position = new Cesium.Cartesian3(-2878389.8418959305,10571075.993566485,4312216.605334455)
				viewer.scene.camera.direction = new Cesium.Cartesian3(0.25560431974019643,-0.9550523331318765,-0.1501381787205059)
				viewer.scene.camera.up = new Cesium.Cartesian3(0.021481994372412105,-0.1496479180597669,0.9885059557423824)
				
				//取消相机变换的所有惯性
				viewer.scene.screenSpaceCameraController.inertiaSpin = 0;
				viewer.scene.screenSpaceCameraController.inertiaTranslate = 0;
				viewer.scene.screenSpaceCameraController.inertiaZoom = 0;
				//去除Logo
				viewer.cesiumWidget.creditContainer.style.display = "none";
				
				viewer.scene.globe.depthTestAgainstTerrain = true;
				viewer.scene.postProcessStages.fxaa.enabled = true;
			
				globeBase = new GlobeBase(viewer)
				provinceView = new ProvinceView(viewer)
				countryView = new CountryView(viewer, provinceView)
				provinceView.loadCountryView(countryView)
				
				loadSource()
			}
			
			function loadSource() {
				countryView && countryView.load()
				globeBase && globeBase.load()
			}
		},
	}
</script>

<style lang="scss">
	.mapViewer {
		position: relative;
		height: 96vh;
		width: 100vw;
	}
</style>